<template>
  <div class="page">
    <!-- header -->
    <van-nav-bar
      :z-index="999"
      :fixed="true"
      left-arrow
      @click-left="onClickLeft"
      :placeholder="true"
      title="详情"
    >
      <template #right>
        <van-icon v-if="favorite" name="star-o" size="18" @click="enshrine" />
        <van-icon
          v-else
          name="star"
          color="rgb(252, 184, 20)"
          size="18"
          @click="cancel"
        />
      </template>
    </van-nav-bar>
    <div class="new-details">
      <div class="new-title">{{ detalis.title }}</div>
      <div class="new-time">
        <p>{{ detalis.deptName }}</p>
        <div>{{ detalis.createTime }}</div>
      </div>
      <p class="new-details-cnr" v-html="detalis.cnr"></p>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import { Collection, CancelCollection } from "../../assets/js/api/apply";
import { getNewDetail } from "../../assets/js/api/News";
import { MyFavorite } from "../../assets/js/api/user.js";
export default {
  data() {
    return {
      text: "",
      detalis: {},
      baseURL: "",
      favorite: true, //是否显示收藏
      title: "", // 标题
      lb: "", // 类别
      id: "" // id
    };
  },

  mounted() {
    if (sessionStorage.getItem("token")) {
      MyFavorite().then(res => {
        // console.log(res);
        let collect = res.data.list;
        for (let i = 0; i < collect.length; i++) {
          if (this.$route.params.id == collect[i].docid) {
            this.favorite = false;
          }
        }
      });
    }

    // console.log(this.$route.params.id);
    getNewDetail(this.$route.params.id).then(res => {
      // console.log(res);
      this.detalis = res.data;
      this.id = this.detalis.id;
      this.lb = this.detalis.lb;
      this.title = this.detalis.title;
    });
  },

  methods: {
    // 收藏
    enshrine() {
      // console.log("收藏");
      let obj = {
        title: this.title,
        lb: this.lb,
        docid: this.id,
        jgbh: ""
      };
      // console.log(obj);
      Collection(obj).then(res => {
        // console.log(res);
        if (res.code == 506) {
          Toast("重复收藏");
        }
        if (res.code == 0) {
          Toast.success("收藏成功");
          this.favorite = false;
        }
      });
    },
    // 取消收藏
    cancel() {
      CancelCollection(this.id).then(res => {
        // console.log(res);
        if (res.code == 0) {
          Toast.success("取消成功");
          this.favorite = true;
        }
      });

      // console.log("取消收藏");
    },

    onClickLeft() {
      this.$router.go(-1); //返回上一层
    }
  },
  created() {
    let dsBridge = require("dsbridge");
    dsBridge.call("goBack", "", function() {
      history.go(-1);
    });
  }
};
</script>
<style></style>
